import {
  getBaseUrl,
  requestUtil
} from '../../utils/requestUtil.js';
Page({
  data: {
      orders: [],
      tabs: [
          {
              id: 0,
              value: "全部订单",
              isActive: true
          },
          {
              id: 1,
              value: "待发货",
              isActive: false
          },
          {
              id: 2,
              value: "待收货",
              isActive: false
          },
          {
              id: 3,
              value: "退款/换货",
              isActive: false
          },
          {
              id: 4,
              value: "待评价",
              isActive: false
          },
          {
              id: 5,
              value: "已完成",
              isActive: false
          }
      ],
      statusMap: {
          1: "待发货",
          2: "待收货",
          3: "退款/换货",
          4: "待评价",
          5: "已完成"
      },
      currentTabIndex: 0,
      indicatorLeft: 0,
      indicatorWidth: 0,
      isLoading: false,
      hasMore: true
  },

  QueryParams: {
      type: 0,
      page: 1,
      pageSize: 100
  },

  totalPage: 1,

  handleItemTap(e) {
      const { index } = e.currentTarget.dataset;
      let { tabs } = this.data;
      tabs.forEach((v, i) => i === index? v.isActive = true : v.isActive = false);

      this.QueryParams.type = index;
      this.QueryParams.page = 1;
      this.setData({
          orders: [],
          currentTabIndex: index,
          tabs
      });
      this.getOrders();
  },

  async getOrders() {
      this.setData({ isLoading: true });
      const res = await requestUtil({ url: '/my/order/list', data: this.QueryParams });
      this.totalPage = res.totalPage;
      this.setData({
          orders: [...this.data.orders, ...res.orderList],
          isLoading: false,
          hasMore: this.QueryParams.page < this.totalPage
      });
  },

  goToHome() {
      wx.switchTab({
          url: '/pages/index/index'
      });
  },

  handleConfirmReceipt(e) {
      const orderId = e.currentTarget.dataset.orderId;
      wx.request({
          url: getBaseUrl() + '/my/order/confirmReceipt/' + orderId,
          method: 'POST',
          success: (res) => {
              if (res.data.code === 0) {
                  wx.showToast({
                      title: '确认收货成功',
                      icon:'success'
                  });
                  let { orders } = this.data;
                  orders = orders.filter(order => order.id!== orderId);
                  this.setData({ orders });
                  this.getOrders();
              } else {
                  wx.showToast({
                      title: '确认收货失败',
                      icon: 'none'
                  });
              }
          }
      });
  },

  handleEvaluate(e) {
      const order = e.currentTarget.dataset.order;
      const orderId = order.id;
      const goodsList = order.goods.map(goods => {
          return {
              productId: goods.goodsId,
              goodsName: goods.goodsName
          };
      });
      wx.navigateTo({
          url: `/pages/evaluation/index?orderId=${orderId}&goodsList=${JSON.stringify(goodsList)}&currentTabIndex=${this.data.currentTabIndex}`
      });
  },

  onLoad(options) {
      const baseUrl = getBaseUrl();
      this.setData({ baseUrl });
  },

  onShow() {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    const { type, currentTabIndex } = currentPage.options;
    if (currentTabIndex!== undefined) {
        this.setCurrentTab(currentTabIndex);
    } else if (type!== undefined) {
        this.QueryParams.type = type;
        this.setCurrentTab(type);
    }
    this.QueryParams.page = 1;
    this.setData({ orders: [] });
    this.getOrders();
},

  setCurrentTab(index) {
      let { tabs } = this.data;
      tabs.forEach((v, i) => i === index? v.isActive = true : v.isActive = false);
      this.QueryParams.type = index;
      this.setData({
          tabs,
          currentTabIndex: index
      });
  },

  onPullDownRefresh() {
      this.QueryParams.page = 1;
      this.setData({
          orders: [],
          hasMore: true
      });
      this.getOrders();
      wx.stopPullDownRefresh();
  },

  onReachBottom() {
      if (this.QueryParams.page < this.totalPage) {
          this.QueryParams.page++;
          this.getOrders();
      } else {
          wx.showToast({
              title: '没有更多订单啦！',
              icon: 'none'
          });
      }
  },

  onShareAppMessage() {

  }
});